package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.Calendar;
import com.jhgsys.internal.sypq.service.ICalendarService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 日历管理 Controller
 *
 * @author ganjun
 * @date 2024-11-21 16:56:51
 */
@Slf4j
@Validated
@Controller
public class CalendarController extends BaseController {

    @Autowired
    private ICalendarService calendarService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "calendar")
    @RequiresPermissions("calendar:view")
    public String calendarIndex(){
        return jumpPage("sypq/calendar/calendarList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "calendar/add")
    @RequiresPermissions("calendar:add")
    public String calendarAdd(){
        return jumpPage("sypq/calendar/calendarEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "calendar/update/{id}")
    @RequiresPermissions("calendar:edit")
    public String calendarUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("calendar", calendarService.findCalendarById(id));
        return jumpPage("sypq/calendar/calendarEdit");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-21 16:56:51
     */
    @GetMapping("calendar")
    @ResponseBody
    @RequiresPermissions("calendar:view")
    public FebsResponse getAllCalendars(Calendar calendar) {
        try {
            return success(calendarService.findCalendarList(calendar));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-21 16:56:51
     */
    @GetMapping("calendar/list")
    @ResponseBody
    @RequiresPermissions("calendar:view")
    public FebsResponse calendarList(QueryRequest request, Calendar calendar) {
        try {
            Map<String, Object> dataTable = getDataTable(this.calendarService.findCalendarPage(request, calendar));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 Calendar
     * @author ganjun
     * @date 2024-11-21 16:56:51
     */
    @Log("新增Calendar")
    @PostMapping("calendar")
    @ResponseBody
    @RequiresPermissions("calendar:add")
    public FebsResponse addCalendar(@Valid @RequestBody Calendar calendar) throws FebsException {
        try {
            this.calendarService.createCalendar(calendar);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 Calendar
     * @author ganjun
     * @date 2024-11-21 16:56:51
     */
    @Log("逻辑删除Calendar")
    @GetMapping("calendar/delete/{id}")
    @ResponseBody
    @RequiresPermissions("calendar:delete")
    public FebsResponse deleteCalendar(@PathVariable("id") String calendarIds) throws FebsException {
        try {
            Calendar calendar = new Calendar();
            calendar.setDeletedBy(getCurrentUser().getUserName());
            calendar.setDeletedTime(new Date());
            calendar.setDeletedFlg(FebsConstant.INVALID);
            this.calendarService.deleteCalendar(calendar,calendarIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 Calendar
    * @author ganjun
    * @date 2024-11-21 16:56:51
    */
    @Log("物理删除Calendar")
    @GetMapping("calendar/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("calendar:delete")
    public FebsResponse physicallyDeleteCalendar(@PathVariable("id") String calendarIds) throws FebsException {
        try {
            this.calendarService.physicallyDeleteCalendar(calendarIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 Calendar
     * @author ganjun
     * @date 2024-11-21 16:56:51
     */
    @Log("修改Calendar")
    @PostMapping("calendar/update")
    @ResponseBody
    @RequiresPermissions("calendar:edit")
    public FebsResponse updateCalendar(Calendar calendar) throws FebsException {
        try {
            this.calendarService.updateCalendar(calendar);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

}
